package com.wn.manpower.web;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.junit.Test;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 流程部署
 * @author dzj
 * date: 2022/10/27
 */
@RestController
@RequestMapping("/manpower/processdeployment")
public class ProcessDeploymentController {


    /**
     * 添加要用的25张表
     */
    @RequestMapping("/addsql")
    public void testCreateDbTable() {
        /**
         * 方式一
         */
        //使用classpath下的activiti.cfg.xml中的配置创建processEngine
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        System.out.println(processEngine);

        /**
         * 方式二
         */
        //先构建ProcessEngineConfiguration
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.
                createProcessEngineConfigurationFromResource("activiti.cfg.xml","processEngineConfiguration");
        //通过ProcessEngineConfiguration创建ProcessEngine，此时会创建数据库
        ProcessEngine processEngine = configuration.buildProcessEngine();
    }

    /**
     * 部署流程定义：调岗申请
     */
    @RequestMapping("/addchpost")
    public void chpost(){
//        1、创建ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        2、得到RepositoryService实例
        RepositoryService repositoryService = processEngine.getRepositoryService();
//        3、使用RepositoryService进行部署
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("bpmn/changeposts.bpmn20.xml") // 添加bpmn资源
//                .addClasspathResource("bpmn/changeposts.png")  // 添加png资源
                .name("调岗申请流程")
                .deploy();
//        4、输出部署信息
        System.out.println("流程部署id：" + deployment.getId());
        System.out.println("流程部署名称：" + deployment.getName());
    }

    /**
     * 部署流程定义：离职申请
     */
    @RequestMapping("/dimission")
    public void dimission(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("bpmn/dimission.bpmn20.xml") // 添加bpmn资源
//                .addClasspathResource("bpmn/changeposts.png")  // 添加png资源
                .name("离职申请流程")
                .deploy();
        System.out.println("流程部署id：" + deployment.getId());
        System.out.println("流程部署名称：" + deployment.getName());
    }

    /**
     * 部署流程定义：办公用品领用
     */
    @RequestMapping("/recipients")
    public void officesupplies(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("bpmn/recipients.bpmn20.xml") // 添加bpmn资源
//                .addClasspathResource("bpmn/changeposts.png")  // 添加png资源
                .name("办公用品申请流程")
                .deploy();
        System.out.println("流程部署id：" + deployment.getId());
        System.out.println("流程部署名称：" + deployment.getName());
    }

    /**
     *流程删除
     */
    @RequestMapping("/delcp")
    public void deleteDeployment() {
        //获取流程引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 通过流程引擎获取repositoryService
        RepositoryService repositoryService = processEngine
                .getRepositoryService();

        // 流程部署id
        String deploymentId = "52501";
        //删除流程定义，如果该流程定义已有流程实例启动则删除时出错
        repositoryService.deleteDeployment(deploymentId);
        //设置true 级联删除流程定义，即使该流程有流程实例启动也可以删除，设置为false非级别删除方式，如果流程
        //repositoryService.deleteDeployment(deploymentId, true);
    }

}
